package cn.csm.jwtlogin.auth;

import cn.csm.jwtlogin.jwt.JwtProvider;
import cn.csm.jwtlogin.jwt.TokenType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * description: HelloRestController <br>
 * date: 2020/10/30 9:27 <br>
 * author: shimi <br>
 * version: 1.0 <br>
 */
@Tag(name = "Auth", description = "认证")
@RestController
@RequestMapping("/auth")
public class AuthController {

    private final JwtProvider jwtProvider;

    public AuthController(JwtProvider jwtProvider) {
        this.jwtProvider = jwtProvider;
    }

    @Operation(summary = "登录")
    @PostMapping("/login")
    public ResponseEntity<LoginResult> login(@RequestBody LoginParam param){
        //check password...
        String token = jwtProvider.createToken(param.getUsername(), 2 * 60 * 60, TokenType.ACCESS);
        String refreshToken = jwtProvider.createToken(param.getUsername(),24 * 60 * 60, TokenType.REFRESH);
        return ResponseEntity.ok(new LoginResult(token,refreshToken, ""));
    }

    @Operation(summary = "获取token")
    @GetMapping("/token")
    public ResponseEntity<String> getToken(@RequestParam String username){
        String token = jwtProvider.createToken(username,2 * 60 * 60, TokenType.ACCESS);
        return ResponseEntity.ok(token);
    }

    @Operation(summary = "刷新token")
    @PutMapping("/refresh")
    public ResponseEntity<LoginResult> refreshToken(@RequestBody String refreshToken){
        boolean b = jwtProvider.validateRefresh(refreshToken);
        Assert.isTrue(b,"refresh_token 无效");
        String subject = jwtProvider.getSubject(refreshToken);
        String token = jwtProvider.createToken(subject, 2 * 60 * 60, TokenType.ACCESS);
        refreshToken = jwtProvider.createToken(subject,24 * 60 * 60, TokenType.REFRESH);
        return ResponseEntity.ok(new LoginResult(token,refreshToken, ""));
    }

    public static class LoginResult{

        private String access_token;
        private String refresh_token;
        private String errmsg;

        public LoginResult(String accessToken, String refreshToken, String errmsg){
            this.access_token = accessToken;
            this.refresh_token = refreshToken;
            this.errmsg = errmsg;
        }

        public String getAccess_token() {
            return access_token;
        }

        public void setAccess_token(String access_token) {
            this.access_token = access_token;
        }

        public String getRefresh_token() {
            return refresh_token;
        }

        public void setRefresh_token(String refresh_token) {
            this.refresh_token = refresh_token;
        }

        public String getErrmsg() {
            return errmsg;
        }

        public void setErrmsg(String errmsg) {
            this.errmsg = errmsg;
        }
    }

    public static class LoginParam{

        private String username;
        private String password;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }

}
